package com.example.qimo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.jinrishici.sdk.android.JinrishiciClient;
import com.jinrishici.sdk.android.factory.JinrishiciFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class allnote extends AppCompatActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {

private static final String Tag = "allnote";

        Myadapter my;
//        GridView list2;
        ListView list2;
        ArrayList<Integer> idlist;
        DBhelper db;

@SuppressLint("Range")
@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_allnote);

        Log.i(Tag,"al");

        db = new DBhelper(this);
        list2=findViewById(R.id.datanote);

//        准备数据


        //创建Values类型的list保存数据库中的数据
        ArrayList<item> valuesList = new ArrayList<item>();
        //获得可读SQLiteDatabase对象
        SQLiteDatabase mydb = db.getReadableDatabase();
         idlist = new ArrayList<>();
        //查询数据库中的数据
        Cursor cursor = mydb.query(DBhelper.TB_NAME,null,null,
                null,null,null,null);
        if(cursor.moveToFirst()){
                Log.i(Tag,"all");
                item values;
                while (!cursor.isAfterLast()){
                        //实例化values对象
                        values = new item();

                        //把数据库中的一个表中的数据赋值给values
                        values.setId(cursor.getInt(cursor.getColumnIndex(DBhelper.ID)));
                        values.setTitle(cursor.getString(cursor.getColumnIndex(DBhelper.TITLE)));
                        values.setText(cursor.getString(cursor.getColumnIndex(DBhelper.CONTENT)));
                        values.setTime(cursor.getString(cursor.getColumnIndex(DBhelper.TIME)));

                        //将values对象存入list对象数组中
                        valuesList.add(values);
                        Log.i(Tag,""+values.getId());
                        idlist.add(values.getId());
                        Log.i(Tag,""+idlist);
                        cursor.moveToNext();
                }
                Log.i(Tag,"a+"+valuesList);
        }
        cursor.close();
        db.close();



        my= new Myadapter(allnote.this, R.layout.allnote_item, valuesList);
        list2.setAdapter(my);
        list2.setEmptyView(findViewById(R.id.no_data));
//      list2.removeView();
        list2.setOnItemClickListener(allnote.this);//单击
        list2.setOnItemLongClickListener(allnote.this);//长按
        }

        public void onback(View btn){
                finish();
        }
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Object item=list2.getItemAtPosition(position);
        Log.i(Tag,"what");
        item it= (item) item;
//        String name=hashMap.get("ItemTitle");
//        String value=hashMap.get("ItemDetail");
        String name = it.getTitle();
        String text = it.getText();
        String time = it.getTime();
        Integer id1 = idlist.get(position);
        Log.i(Tag,"what"+id1);

        Intent intent=new Intent(allnote.this,show_one_note_detail.class);
//        Bundle bundle=new Bundle();
//        bundle.putString("n_title",name);
//        bundle.putString("n_text",text);
//        bundle.putString("n_time",time);
//        bundle.putString("n_id",""+id1);
//
//        intent.putExtras(bundle);

        intent.putExtra("n_title",name);
        intent.putExtra("n_text",text);
        intent.putExtra("n_time",time);
        intent.putExtra("n_id",""+id1);

        Log.i(Tag,"n_title"+name);
        Log.i(Tag,"n_text"+text);
        Log.i(Tag,"n_id"+id1);
        startActivity(intent);
        }
        public void onClickback(View btn) {

                Intent intent = new Intent(this,MainActivity.class);
                startActivity(intent);
        }


@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int pos, long l) {
        Log.i(Tag,"changan");
        AlertDialog.Builder builder = new AlertDialog.Builder(allnote.this);
        builder.setTitle(R.string.long_edit_title).setMessage(R.string.long_edit_text).
        setPositiveButton(R.string.long_edit_Yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//        只删除显示列表，不删除数据库
        my.remove(list2.getItemAtPosition(pos));
        my.notifyDataSetChanged();
//        删除数据库
        SQLiteDatabase db1 = db.getWritableDatabase();
        db1.delete(DBhelper.TB_NAME,DBhelper.ID+"=?",new String[]{String.valueOf(idlist.get(pos))});
        db1.close();
        }

        }).setNegativeButton(R.string.long_edit_NO,null);
        builder.create().show();
        Log.i(Tag,"long click to delete"+list2.getScrollBarSize());
        return true; //长按发生后不会相应单击，屏蔽单击事件
        }
        }


